package com.example.riceexambackend.service.impl;

import com.example.riceexambackend.common.errorcode.ServiceErrorCodeConstants;
import com.example.riceexambackend.common.exception.ServiceException;
import com.example.riceexambackend.controller.param.questiontype.QuestionTypeAddParam;
import com.example.riceexambackend.controller.param.questiontype.QuestionTypeDeleteParam;
import com.example.riceexambackend.controller.param.questiontype.QuestionTypeUpdateParam;
import com.example.riceexambackend.dao.dataobject.QuestionTypeDO;
import com.example.riceexambackend.dao.mapper.QuestionTypeMapper;
import com.example.riceexambackend.service.QuestionTypeService;
import com.example.riceexambackend.service.dto.questiontype.QuestionTypeAddDTO;
import com.example.riceexambackend.service.dto.questiontype.QuestionTypeDeleteDTO;
import com.example.riceexambackend.service.dto.questiontype.QuestionTypeSelectDTO;
import com.example.riceexambackend.service.dto.questiontype.QuestionTypeUpdateDTO;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class QuestionTypeServiceImpl implements QuestionTypeService {

    @Resource
    private QuestionTypeMapper questionTypeMapper;

    @Override
    public QuestionTypeDeleteDTO deleteQuestionType(QuestionTypeDeleteParam param) {
        // 1. 题型id非空校验
        Long questionTypeId = param.getQuestionTypeId();
        if (questionTypeId == null) {
            throw new ServiceException(ServiceErrorCodeConstants.QUESTION_TYPE_DELETE_PARAM_NULL);
        }
        // 2. 具体参数校验
        if (questionTypeId <= 0) {
            throw new ServiceException(ServiceErrorCodeConstants.QUESTION_TYPE_DELETE_PARAM_ERROR);
        }
        // 3. 查询待删除记录是否存在
        QuestionTypeDO dbDO = questionTypeMapper.selectById(questionTypeId);
        if (dbDO == null) {
            throw new ServiceException(ServiceErrorCodeConstants.QUESTION_TYPE_DELETE_PARAM_ERROR);
        }
        // 4. 逻辑删除数据库记录
        questionTypeMapper.deleteById(questionTypeId);
        // 5. 返回响应
        QuestionTypeDeleteDTO questionTypeDeleteDTO = new QuestionTypeDeleteDTO();
        questionTypeDeleteDTO.setQuestionTypeId(questionTypeId);
        return questionTypeDeleteDTO;
    }

    @Override
    public QuestionTypeUpdateDTO updateQuestionType(QuestionTypeUpdateParam param) {
        // 1. 题型id、题型名称、题型分数非空校验
        Long questionTypeId = param.getQuestionTypeId();
        String questionTypeName = param.getQuestionName();
        Byte questionTypeScore = param.getQuestionScore();
        if (questionTypeId == null) {
            throw new ServiceException(ServiceErrorCodeConstants.QUESTION_TYPE_UPDATE_PARAM_NULL);
        }
        if (StringUtils.isBlank(questionTypeName)) {
            throw new ServiceException(ServiceErrorCodeConstants.QUESTION_TYPE_UPDATE_PARAM_NULL);
        }
        if (questionTypeScore == null) {
            throw new ServiceException(ServiceErrorCodeConstants.QUESTION_TYPE_UPDATE_PARAM_NULL);

        }
        // 2. 具体校验
        if (questionTypeName.length() > 8) {
            throw new ServiceException(ServiceErrorCodeConstants.QUESTION_TYPE_UPDATE_PARAM_ERROR);
        }
        if (questionTypeScore > 100 || questionTypeScore <= 0) {
            throw new ServiceException(ServiceErrorCodeConstants.QUESTION_TYPE_UPDATE_PARAM_ERROR);
        }
        if (questionTypeId <= 0) {
            throw new ServiceException(ServiceErrorCodeConstants.QUESTION_TYPE_UPDATE_PARAM_ERROR);
        }
        QuestionTypeDO dbDO = questionTypeMapper.selectById(questionTypeId);
        if (dbDO == null) {
            // 修改记录不存在
            throw new ServiceException(ServiceErrorCodeConstants.QUESTION_TYPE_UPDATE_PARAM_ERROR);
        }
        // 3. 修改数据库
        QuestionTypeDO questionTypeDO = new QuestionTypeDO();
        questionTypeDO.setId(questionTypeId);
        questionTypeDO.setQuestionName(questionTypeName);
        questionTypeDO.setQuestionScore(questionTypeScore);
        questionTypeMapper.updateById(questionTypeDO);
        QuestionTypeUpdateDTO questionTypeUpdateDTO = new QuestionTypeUpdateDTO();
        questionTypeUpdateDTO.setQuestionTypeId(questionTypeId);
        return questionTypeUpdateDTO;
    }

    @Override
    public List<QuestionTypeSelectDTO> selectAllQuestionType() {
        // 查询全部数据
        List<QuestionTypeDO> questionTypeDOList = questionTypeMapper.selectList();
        // 返回给前端
        List<QuestionTypeSelectDTO> questionTypeSelectDTOList = new ArrayList<>();
        for (QuestionTypeDO questionTypeDO : questionTypeDOList) {
            QuestionTypeSelectDTO questionTypeSelectDTO = new QuestionTypeSelectDTO();
            questionTypeSelectDTO.setId(questionTypeDO.getId());
            questionTypeSelectDTO.setQuestionName(questionTypeDO.getQuestionName());
            questionTypeSelectDTO.setQuestionScore(questionTypeDO.getQuestionScore());
            questionTypeSelectDTOList.add(questionTypeSelectDTO);
        }
        return questionTypeSelectDTOList;
    }

    @Override
    public QuestionTypeAddDTO insertQuestionType(QuestionTypeAddParam param) {
        // 0. 参数校验
        if (param == null) {
            throw new ServiceException(ServiceErrorCodeConstants.QUESTION_TYPE_ADD_PARAM_NULL);
        }
        // 1. 题型名称、题型分数非空校验
        String questionTypeName = param.getQuestionName().trim();
        byte questionTypeScore = param.getQuestionScore();
        if (StringUtils.isBlank(questionTypeName)) {
            throw new ServiceException(ServiceErrorCodeConstants.QUESTION_TYPE_ADD_PARAM_ERROR);
        }
        // 2. 题型名称 <= 8位
        if (questionTypeName.length() > 8) {
            throw new ServiceException(ServiceErrorCodeConstants.QUESTION_TYPE_ADD_PARAM_ERROR);
        }
        // 3. 1 <= 题型分数 <= 100
        if (questionTypeScore < 1 || questionTypeScore > 100) {
            throw new ServiceException(ServiceErrorCodeConstants.QUESTION_TYPE_ADD_PARAM_ERROR);
        }
        // 4. 题型名称不得重复
        QuestionTypeDO dbDO = questionTypeMapper.selectByName(questionTypeName);
        if (dbDO != null) {
            throw new ServiceException(ServiceErrorCodeConstants.QUESTION_TYPE_ADD_NAME_EXISTS);
        }
        // 5. 插入到数据库中
        QuestionTypeDO questionTypeDO = new QuestionTypeDO();
        questionTypeDO.setQuestionName(questionTypeName);
        questionTypeDO.setQuestionScore(questionTypeScore);
        questionTypeMapper.insertQuestionType(questionTypeDO);
        // 6. 返回响应
        QuestionTypeAddDTO questionTypeAddDTO = new QuestionTypeAddDTO();
        questionTypeAddDTO.setQuestionTypeId(questionTypeDO.getId());
        return questionTypeAddDTO;
    }
}
